package com.oritsh.dicom.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oritsh.dicom.utils.adaptor.JsonAdaptorHelper;
import org.dcm4che.data.*;
import org.dcm4che.io.DicomInputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: zarra
 * Date: 14-2-5
 * Time: 下午8:49
 * Copyright Shanghai Orient Rain Information Technology Co.,Ltd.
 */
public class DataSetHelper {
    static ElementDictionary elementDictionary  =
            StandardElementDictionary.getStandardElementDictionary();
    public static void putValueToDataSet(Attributes dataset,int tag,Object value){
        VR vr = elementDictionary.vrOf(tag);
        if(value == null){
            dataset.setNull(tag, vr);
        }
        else if(vr == VR.DA){
            DateFormat format = new SimpleDateFormat("yyyyMMdd");
            dataset.setString(tag, vr,format.format(value));
        }
        else if (vr == VR.TM){
            DateFormat format = new SimpleDateFormat("HHmmss");
            dataset.setString(tag, vr, format.format(value));
        }
        else if(vr.isStringType()){
            dataset.setString(tag, vr, value.toString());
        }
        else if(vr.isIntType() && value instanceof Integer){
            Integer iInt = (Integer)value;
            dataset.setInt(tag, vr, iInt);
        }
        else if(value instanceof Float){
            Float f = (Float)value;
            dataset.setFloat(tag, vr, f);
        }
        else{
            byte[] bytes = vr.toBytes(value, SpecificCharacterSet.DEFAULT);
            dataset.setBytes(tag, vr, bytes);
        }
    }
    static public Attributes createDataSetForNullTags(int[] tags){
        Attributes attributes = new Attributes();
        for(int tag:tags){
            putValueToDataSet(attributes,tag,null);
        }
        return attributes;
    }

    static public String attributesToString(Attributes attributes) throws JsonProcessingException {
        ObjectMapper mapper = JsonAdaptorHelper.objectMapper();
        return mapper.writeValueAsString(attributes);
    }

    static public String attributesToString(Attributes[] attributes) throws JsonProcessingException {
        ObjectMapper mapper = JsonAdaptorHelper.objectMapper();
        return mapper.writeValueAsString(attributes);
    }

    static public Attributes attributesFromInputStream(InputStream inputStream) throws IOException {
        try(DicomInputStream dicomInputStream = new DicomInputStream(inputStream)){
            Attributes fmi = dicomInputStream.readFileMetaInformation();
            Attributes ds  = dicomInputStream.readDataset(-1,-1);
            return ds;
        }
    }

    static public Attributes attributesFromFile(File file) throws IOException {
        try(FileInputStream fileInputStream = new FileInputStream(file)){
            return attributesFromInputStream(fileInputStream);
        }
    }
}
